<template>
  <div>
    <!-- 上部分显示登录状态，加载对应模块 -->
    <div class="header not-login" v-if="!userToken">
      <div class="unlogin" @click="tologin">
        <img src="~@/assets/mobile.png" alt />
        <span>登录/去注册</span>
      </div>
    </div>
    <div class="header done-login" v-else>
      <div class="user-info">
        <div class="left">
          <van-image class="user-img" :src="userInfo.photo" alt round />
          <span>可爱小法法</span>
        </div>
        <div class="right">
          <van-button type="info" round size="mini">编辑资料</van-button>
        </div>
      </div>
      <div class="data-count">
        <div class="data-count-item">
          <span class="count">{{userInfo.art_count}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-count-item">
          <span class="count">{{userInfo.follow_count}}</span>
          <span class="text">关注</span>
        </div>
        <div class="data-count-item">
          <span class="count">{{userInfo.fans_count}}</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-count-item">
          <span class="count">{{userInfo.like_count}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 宫格导航 -->
    <van-grid clickable :column-num="2" class="my-grid">
      <van-grid-item icon="home-o" text="历史" to="/">
        <template #icon>
          <i class="iconfont iconlishi"></i>
        </template>
        <span slot="text">历史</span>
      </van-grid-item>
      <van-grid-item icon="search" text="收藏" to="/">
        <template #icon>
          <i class="iconfont iconshoucang"></i>
        </template>
        <span slot="text">收藏</span>
      </van-grid-item>
    </van-grid>
    <!-- 消息通知和小智同学 -->
    <div class="box">
      <van-cell title="消息通知" is-link class="meassage nav" />
      <van-cell title="小智同学" is-link class="xiaozhi nav" />
    </div>
    <!-- 退出登录 -->
    <van-cell class="logout" v-if="userToken" @click="logout" clickable>
      <span slot="title">退出登录</span>
    </van-cell>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  data() {
    return {
      userInfo: {}
    }
  },
  created() {
    // 渲染用户信息
    if (!this.userToken) return
    this.loadUserInfo()
  },
  methods: {
    tologin() {
      this.$router.push('/login')
    },
    logout() {
      // 退出清理token
      // 确认退出弹出框
      this.$dialog
        .confirm({
          message: '确认退出？'
        })
        .then(() => {
          // 确认删除token,本地和容器都变了
          this.$store.commit('saveToken', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    async loadUserInfo() {
      // 获取用户信息
      try {
        const { data: res } = await getUserInfo()
        console.log(res.data)
        this.userInfo = res.data
      } catch (err) {
        console.log(err)
        this.$toast.fail('获取用户数据失败')
      }
    }
  },
  computed: {
    ...mapState(['userToken'])
  }
}
</script>

<style lang='less' scoped>
.header {
  height: 361px;
  background: url('~@/assets/banner.png') no-repeat;
  background-size: cover;
}
.not-login {
  display: flex;
  align-items: center;
  justify-content: center;
  .unlogin {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      margin-top: 15px;
      font-size: 28px;
      color: #fff;
    }
  }
}
.done-login {
  .user-info {
    box-sizing: border-box;
    padding: 76px 32px 23px;
    height: 231px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      display: flex;
      text-align: left;
      align-items: center;
      .user-img {
        width: 132px;
        height: 132px;
      }
      span {
        font-size: 30px;
        margin-left: 20px;
        color: #fff;
      }
    }
  }
  .data-count {
    display: flex;
    align-items: center;
    .data-count-item {
      height: 130px;
      flex: 1;
      display: flex;
      align-items: center;
      flex-direction: column;
      justify-content: center;
      color: #fff;
      .count {
        font-size: 36px;
      }
      .text {
        font-size: 23px;
      }
    }
  }
}
.my-grid {
  .iconfont {
    font-size: 45px;
  }
  .iconshoucang {
    color: #eb5253;
  }
  .iconlishi {
    color: #ff9d1d;
  }
  span {
    margin-top: 15px;
    font-size: 28px;
  }
}
.box {
  margin-top: 10px;
  .nav {
    font-size: 30px;
    font-family: 'Microsoft YaHei';
    color: rgb(51, 51, 51);
    height: 105px;
  }
}
.logout {
  position: relative;
  margin-top: 10px;
  height: 107px;
  display: flex;
  span {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    color: #de4040;
    font-size: 30px;
  }
}
</style>
